<template>
  <div class="step-container">
    <template v-if="certType == '1'">
      <Word1
          ref="Word1"
          v-if="bussType == 1"
          :formData="formData"
          :disabled="disabled"
          @loading="handleLoading"
      />
      <Word2
          ref="Word2"
          v-else-if="bussType == 2"
          :formData="formData"
          :disabled="disabled"
          @loading="handleLoading"
      />
      <Word3
          ref="Word3"
          v-else-if="bussType == 3"
          :formData="formData"
          :disabled="disabled"
          @loading="handleLoading"
      />
      <Word4
          ref="Word4"
          v-else-if="bussType == 4"
          :formData="formData"
          :disabled="disabled"
          @loading="handleLoading"
      />
    </template>
    <template v-else-if="certType == '3'">
      <SafeWord1
          ref="SafeWorda"
          v-if="bussType == 'a'"
          :formData="formData"
          :disabled="disabled"
          @loading="handleLoading"
      />
      <SafeWord2
          ref="SafeWordb"
          v-else-if="bussType == 'b'"
          :formData="formData"
          :disabled="disabled"
          @loading="handleLoading"
      />
      <SafeWord3
          ref="SafeWordc"
          v-else-if="bussType == 'c'"
          :formData="formData"
          :disabled="disabled"
          @loading="handleLoading"
      />
      <SafeWord4
          ref="SafeWordd"
          v-else-if="bussType == 'd'"
          :formData="formData"
          :disabled="disabled"
          @loading="handleLoading"
      />
    </template>
  </div>
</template>
<script>
import Word1 from './components/Word1';
import Word2 from './components/Word2';
import Word3 from './components/Word3';
import Word4 from './components/Word4';
import SafeWord1 from './components/safe-type/Word1';
import SafeWord2 from './components/safe-type/Word2';
import SafeWord3 from './components/safe-type/Word3';
import SafeWord4 from './components/safe-type/Word4';
import businessApi from '@/api/business';
import userApi from '@/api/user';
import { decrypt } from '@/utils/sms4util';

export default {
  props: {
    id: {
      type: String,
    },
    bussType: {
      default: '1',
    },
    disabled: {
      default: false,
    },
    userId: {
      type: String,
    },
    certType: {
      default: '1',
    }
  },
  components: { Word1, Word2, Word3, Word4, SafeWord1, SafeWord2, SafeWord3, SafeWord4 },
  data() {
    return {
      formData: {},
    };
  },
  mounted() {
    console.log(this.bussType, this.certType)
    this.init();
  },
  methods: {
    async init() {
      // 获取用户信息
      this.$emit('loading', true);
      await this.initUserInfo();
      if (this.id) {
        let params = {
          id: this.id,
          type: this.bussType,
        };
        // 获取业务信息
        const res = await businessApi.getStepsInfo(params);
        this.formData = {
          ...this.formData,
          ...res,
          phone: res.phone ? decrypt(res.phone) : undefined,
          paperCode: res.paperCode ? decrypt(res.paperCode) : undefined,
          sqrTel: res.sqrTel ? decrypt(res.sqrTel) : undefined,
          jbrTel: res.jbrTel ? decrypt(res.jbrTel) : undefined,
        };
        // 根据业务类型处理数据
        if (this.certType == '1') {
          this.$refs[`Word${this.bussType}`].checkValue1 =
              this.formData.paperType;
          if (this.bussType == 3) {
            this.$refs[`Word${this.bussType}`].checkValue2 =
                +this.formData.bgType;
          }
        }
      } else {
        // 获取旧证信息
        await this.initOldCert();
      }
      this.$emit('loading', false);
    },
    async initUserInfo() {
      let params = {
        id: this.userId || this.$store.state.user.userInfo.id,
        type: this.certType,
      };
      // 获取用户信息
      const res = await userApi.getUserInfo(params);
      const paperCode = res.paperCode ? decrypt(res.paperCode) : '';
      const birthday =
        res.paperType === '111' && paperCode
          ? new Date(
              `${paperCode.slice(6, 10)}-${paperCode.slice(
                10,
                12,
              )}-${paperCode.slice(12, 14)}`,
            )
          : undefined;
      this.formData = {
        name: res.realname,
        school: res.school,
        graduatedTime: res.graduatedTime,
        major: res.major,
        education: res.education,
        degree: res.degree,
        idPhoto: res.idPhoto,
        paperCertCode: res.paperCertCode,
        paperCode: res.paperType === '111' ? paperCode : undefined,
        sex:
          res.paperType === '111' && paperCode
            ? +paperCode.slice(16, 17) % 2
              ? '1'
              : '2'
            : undefined,
        birthday,
        titleLevel: res.titleLevel,
        paperType: '111',
        bgType: 1,
      };
    },
    // 获取旧证信息
    async initOldCert() {
      if (this.certType != '3') {
        let params = {
          type: this.bussType,
          userId: this.userId || this.$store.state.user.userInfo.id,
        };
        const res = await businessApi.getOldCertInfo(params);
        if (res) {
          this.formData = Object.assign({}, {
            ...res,
            paperCode: res.paperCode ? decrypt(res.paperCode) : undefined,
            sqrTel: res.sqrTel ? decrypt(res.sqrTel) : undefined,
            jbrTel: res.jbrTel ? decrypt(res.jbrTel) : undefined,
            phone: res.phone ? decrypt(res.phone) : undefined,
          }, this.formData);
        }
      }
    },
    //暂存、提交
    async save(status = 0) {
      if (!this.validateForm()) {
        return Promise.resolve({});
      }
      let params = {
        ...this.formData,
        ...this.$refs[this.certType == '1' ? `Word${this.bussType}` : `SafeWord${this.bussType}`].tempFormData,
      };
      if (this.id) {
        params.applyId = this.id;
      }
      // 保存业务信息
      // 二级造价
      this.$emit('loading', true);
      if (this.certType == 1) {
        const res = await businessApi[`saveUserBusiness${this.bussType}`](
            status,
            params,
        );
        this.$emit('loading', false);
        this.$message.success('保存成功');
        return Promise.resolve({
          infoId: res,
        });
        // 安全管理
      } else if (this.certType == 3) {
        const res = await businessApi[`saveUserBusiness${this.bussType}`](
            status,
            params,
        ).finally(() => {
          this.$emit('loading', false);
        });
        this.$message.success('保存成功');
        return Promise.resolve({
          infoId: res,
        });
      }
    },
    // 根据业务类型校验表单
    validateForm() {
      if (this.certType == '1') {
        return this.$refs[`Word${this.bussType}`].validateForm();
      } else if (this.certType == '3') {
        return this.$refs[`SafeWord${this.bussType}`].validateForm();
      } else {
        return false;
      }
    },
    // 加载中
    handleLoading(status) {
      this.$emit('loading', status);
    },
  },
};
</script>
<style lang="scss" scoped>
.step-container {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .text {
    width: 100%;
    margin-top: 18px;
    font-size: 14px;
  }
  .check {
    margin-top: 40px;
  }
}
</style>
